import java.util.Collection;
import java.util.HashMap;

/**
 * @author CY
 * @date 2022/10/31 16:52
 **/
public class Test2 {
    class Solution {
        public boolean findNumberIn2DArray(int[][] arr, int target) {
            if (arr.length == 0 || arr == null) {
                return false;
            }
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].length != 0) {
                    if (target <= arr[i][arr[i].length - 1]) {
                        for (int j = 0; j < arr[i].length; j++) {
                            if (arr[i][j] == target) {
                                return true;
                            }
                        }
                    }

                }

            }
            return false;
        }
    }
    public boolean containsNearbyDuplicate(int[] arr, int k) {
        if(arr==null||arr.length==0){
            return false;
        }
        HashMap<Integer,Integer> hm= new HashMap<Integer,Integer>();
        for(int i=0;i<arr.length;i++){
            if(!hm.containsKey(arr[i])){
                hm.put(arr[i],i);
            }else{
                Collection<Integer> collection=hm.values();
                for(Integer j:collection){
                    if(i==j&&Math.abs(j-i)<=k){
                        return false;
                    }
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        int arr[]={1,0,1,1};
        System.out.println(test2.containsNearbyDuplicate(arr, 1));


    }
}
